// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-11-8

#ifndef __NET_ACCEPT_H_COMMON_SERVLIB__
#define __NET_ACCEPT_H_COMMON_SERVLIB__

#pragma once

#include "net_socket.h"
#include "net_overlapped.h"

namespace JoyServer
{

class CJoyAcceptOverlapped : public CJoyOverlapped
{
public:
    SOCKET m_acceptSocket;
    unsigned char m_outputBuffer[2*(sizeof(sockaddr_in)+16)];

    virtual void OnComletion(DWORD dwTransferred, DWORD err);
public:
    CJoyAcceptOverlapped(CJoySockData* sockData, SOCKET acceptSocket);
    virtual ~CJoyAcceptOverlapped();
};

// listen at the port.
// use AcceptEx to post asynchronous accept operations.
class CJoyAcceptorEx
    : public CJoySocket
{
protected:
    friend CJoyAcceptOverlapped;

    LPFN_GETACCEPTEXSOCKADDRS   m_pfnGetAcceptExAddrs;
    LPFN_ACCEPTEX   m_pfnAcceptEx;

    virtual bool OnAccept(SOCKET s, const sockaddr_in& addr);

    bool QueueAccept(int& err);
    bool QueueAccept();
public:
    CJoyAcceptorEx();
    virtual ~CJoyAcceptorEx();

    virtual bool Attach(SOCKET s, bool bStream, int& err);

    // acceptCount is the posted operations count.
    bool Start(int acceptCount, int& err);
    bool Start(int acceptCount);
};

} // namespace JoyServer

#endif  // __NET_ACCEPT_H_COMMON_SERVLIB__